//
//  HomeShopView.swift
//  VastSwift
//
//  Created by ALH000355 on 2022/2/16.
//


import UIKit

// 自选商城view-包括自选商城、轮胎直购、二手车件
class HomeShopView: UIView {
    
    // 点击view回调事件
    var callback: ClickEnquiryViewSure!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.uiConfig()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    func uiConfig() -> Void {
        let width = (screenWidth - 6) / 3
        let first = HomeShopDetailView.init(frame: CGRect.init(x: 0, y: 0, width: width, height: 123))
        first.showViewInfo(text: "自选商城", image: "zixuanshangpin") {
            if let callback1 = self.callback {
                callback1(1)
            }
        }
        self.addSubview(first)
        let second = HomeShopDetailView.init(frame: CGRect.init(x: width + 3, y: 0, width: width, height: 123))
        second.showViewInfo(text: "轮胎直购", image: "luntaizhigou") {
            if let callback1 = self.callback {
                callback1(2)
            }
        }
        self.addSubview(second)
        let third = HomeShopDetailView.init(frame: CGRect.init(x: width * 2 + 6, y: 0, width: width, height: 123))
        third.showViewInfo(text: "二手车/件", image: "ershouchejian") {
            if let callback1 = self.callback {
                callback1(3)
            }
        }
        self.addSubview(third)
    }
}
// 最小的单元格,宽70，高90
class HomeShopDetailView: UIView {
    
    // 展示的图片
    var showImageView: UIImageView!
    // 标题
    var titleLabel: UILabel!
    var arrowImageView: UIImageView!
    
    // 点击view回调事件
    private var tap: ClickViewSure?
    
    func showViewInfo(text: String, image: String, handle CompletionHandle: @escaping ClickViewSure) {
        titleLabel.text = text

        showImageView.image = UIImage.init(named: image)
        tap = CompletionHandle
    }
    @objc func clickView() {
        if let click = tap {
            click()
        }
    }
    
    func uiConfig() -> Void {
        self.backgroundColor = .white
        showImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 37, width: width, height: height - 37))
        showImageView.backgroundColor = .clear
        showImageView.isUserInteractionEnabled = true
        
        arrowImageView = UIImageView.init(frame: CGRect.init(x: width - 30, y: 14, width: 18, height: 18))
        arrowImageView.backgroundColor = .clear
        arrowImageView.isUserInteractionEnabled = true
        arrowImageView.image = UIImage.init(named: "icon_xia")
        
        titleLabel = UILabel.init(frame: CGRect.init(x: 15, y: 12, width: 85, height: 22))
        titleLabel.textAlignment = .left
        titleLabel.textColor = .black
        titleLabel.font = .systemFont(ofSize: 16)

        self.addSubview(showImageView)
        self.addSubview(titleLabel)
        self.addSubview(arrowImageView)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickView))
        self.addGestureRecognizer(tap)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.uiConfig()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
